import messages from './messages'
import { createI18n } from 'vue-i18n'

function transformMessages(messages: object, locale: string) {
  // 递归遍历所有的翻译项，并根据 locale 返回当前语言的翻译
  function transform(obj: object) {
    const result: Record<string, any> = {}
    for (const [key, value] of Object.entries(obj)) {
      if (typeof value === 'object' && !Array.isArray(value)) {
        if (value[locale]) {
          result[key] = value[locale]
        }
        else {
          result[key] = transform(value)
        }
      }
      else {
        result[key] = value
      }
    }
    return result
  }

  return transform(messages)
}

const en = transformMessages(messages, 'en')
const cn = transformMessages(messages, 'cn')
const jp = transformMessages(messages, 'jp')

const i18n = createI18n({
  locale: 'cn',
  messages: {
    en,
    cn,
    jp,
  },
})
export default i18n
